package com.youren.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.youren.health.dao.MemberDao;
import com.youren.health.dao.OrderDao;
import com.youren.health.dao.OrderSettingDao;
import com.youren.health.exception.HealthException;
import com.youren.health.pojo.Member;
import com.youren.health.pojo.Order;
import com.youren.health.pojo.OrderSetting;
import com.youren.health.service.OrderService;
import com.youren.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-30 11:23
 **/
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    @Transactional
    public Integer submit(Map<String, String> paraMap) throws HealthException{
        String telephone = paraMap.get("telephone");
        String dateString = paraMap.get("orderDate");
        String setmealId = paraMap.get("setmealId");
        Order order=new Order();
        Date orderDate = null;
        try {
            orderDate = DateUtils.parseString2Date(dateString);
        } catch (Exception e) {
            throw new HealthException("日期格式不正确");
        }
        //1.查询用户所选择的预约日期是否接受预约
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting==null) {
            throw new HealthException("当天不接受预约");
        }
        //2.查询所选预约日期是否已经约满
        if (orderSetting.getReservations()==orderSetting.getNumber()) {
            throw new HealthException("当天预约名额已满");
        }
        order.setOrderDate(orderDate);
        order.setSetmealId(Integer.valueOf(setmealId));
        //3.检查用户是否是会员
        Member member = memberDao.findByPhone(telephone);
        if (member==null){
            //不是则注册后进行预约
            member=new Member();
            member.setName(paraMap.get("name"));
            member.setSex(paraMap.get("sex"));
            member.setPhoneNumber(telephone);
            member.setIdCard(paraMap.get("idCard"));
            member.setRegTime(new Date());
            memberDao.add(member);
            order.setMemberId(member.getId());
        }else {

            //检查当天是否已预约了同个套餐
            order.setMemberId(member.getId());
            Order order1 = orderDao.findOrder(order);
            if (order1!=null){
                throw new HealthException("您已预约了该套餐，请勿重复预约");
            }
        }

        //更新当日已预约人数
        Integer count = orderSettingDao.updateNumber(orderSetting);
        if (count==0){
            throw new HealthException("预约已满，请选择其它日期");
        }

        //添加订单
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        orderDao.add(order);
        return order.getId();
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        return orderDao.findById(id);
    }
}
